package mwa.cs.util;
import java.util.Properties;
import javax.mail.PasswordAuthentication;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import mwa.cs.model.MailProperties;



public class Mail {
   
	public void sendMail(MailProperties voMailProperties){
	 
		final String vaUsername = voMailProperties.getVaMailSender();
        final String vaPassword = voMailProperties.getVaMailSenderPassword();
        final String vaUser = voMailProperties.getVaMailReceiver();
        
        Properties voProps = new Properties();
        //props.put("mail.smtp.starttls.enable", "true");
        voProps.put("mail.smtp.auth", voMailProperties.getVaMailAuth());
        voProps.put("mail.smtp.host", voMailProperties.getVaMailHost());
        voProps.put("mail.smtp.port", voMailProperties.getVaMailPort());

        Session session = Session.getInstance(voProps,
          new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(vaUsername, vaPassword);
            }
          });
    
        try {
        
        	MimeMessage voMessage = new MimeMessage(session);
            voMessage.setFrom(new InternetAddress(vaUsername));
            voMessage.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(vaUser));//Destination mail
            voMessage.setSubject(voMailProperties.getVaMailSubject(),"UTF-8");
            voMessage.setContent(voMailProperties.getVaMailText(), "text/html; charset=utf-8");
            Transport.send(voMessage);
            System.out.println("Done");

        } catch (MessagingException e) {
        	throw new RuntimeException(e); 
        }		
		    
	}

	public void sendFromGmail(MailProperties voMailProperties){
		
		final String vaUsername = "thapakit2@gmail.com";
        final String vapassword = "The-Thapakit";
        final String vaUser = voMailProperties.getVaMailReceiver();
        
        String vaSslFactory = "javax.net.ssl.SSLSocketFactory"; 
        
        Properties voProps = new Properties();
        voProps.put("mail.host", "smtp.gmail.com"); 
        voProps.put("mail.transport.protocol.", "smtp"); 
        voProps.put("mail.smtp.auth", "true"); 
        voProps.put("mail.smtp.", "true"); 
        voProps.put("mail.smtp.port", "465"); 
        voProps.put("mail.smtp.socketFactory.fallback", "false"); 
        voProps.put("mail.smtp.socketFactory.class", vaSslFactory); 

        Session session = Session.getInstance(voProps,
          new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(vaUsername, vapassword);
            }
          });
    
        try {
        
        	MimeMessage voMessage = new MimeMessage(session);
            voMessage.setFrom(new InternetAddress(vaUsername));
            voMessage.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(vaUser));//Destination mail
            voMessage.setSubject(voMailProperties.getVaMailSubject(),"UTF-8");
            voMessage.setContent(voMailProperties.getVaMailText(), "text/html; charset=utf-8");
            Transport.send(voMessage);
            System.out.println("Done");

        } catch (MessagingException e) {
        	throw new RuntimeException(e); 
        }		
		
	}

}
